package ucadminlogic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"go.uber.org/zap"
	"navi-mumbai/app/rpc/model/pg/usercenter"
	"navi-mumbai/app/rpc/usercenter/internal/svc"
	"navi-mumbai/app/rpc/usercenter/pb"
	"navi-mumbai/common/key/dbkey"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"
)

type UpdateMarqueeConfigLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateMarqueeConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateMarqueeConfigLogic {
	return &UpdateMarqueeConfigLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 创建MarqueeConfig设置
func (l *UpdateMarqueeConfigLogic) UpdateMarqueeConfig(in *pb.UpdateMarqueeConfigReq) (*pb.UpdateMarqueeConfigResp, error) {
	defer utils.RecoverPanic()

	playStartDate, err := utils.ParseTimeByString(in.PlayStartDate, dbkey.DateFormat)
	playEndDate, err := utils.ParseTimeByString(in.PlayEndDate, dbkey.DateFormat)
	if err != nil {
		l.Logger.Errorf("CreateMarqueeConfig. time == nil, err = ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.ParseTimeError, err.Error())
	}
	uf := usercenter.MarqueeConfig{
		Id:             in.Id,
		MarqueeNo:      in.MarqueeNo,
		MarqueeContent: in.MarqueeContent,
		MarqueeSpeed:   in.MarqueeSpeed,
		PlayStartDate:  *playStartDate,
		PlayEndDate:    *playEndDate,
		PlayTimes:      in.PlayTimes,
		Enable:         in.Enable,
	}
	err = l.svcCtx.MarqueeConfigModel.Update(l.ctx, &uf)
	if err != nil {
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	return &pb.UpdateMarqueeConfigResp{}, nil
}
